<template>
  <div class="login_box">
    <div class="form_box">
      <el-form
        ref="ruleFormRef"
        :model="ruleForm"
        status-icon
        :rules="rules"
        label-width="80px"
        class="demo-ruleForm"
        label-position="left"
      >
        <h2 class="title">后台管理系统</h2>
        <el-form-item label="账号：" prop="username">
          <el-input v-model.number="ruleForm.username" />
        </el-form-item>
        <el-form-item label="密码：" prop="passworld">
          <el-input
            v-model="ruleForm.passworld"
            type="password"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            class="login_btn"
            type="primary"
            @click="submitForm(ruleFormRef)"
            >Submit</el-button
          >
          <el-button class="login_btn" @click="resetForm(ruleFormRef)"
            >Reset</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref, toRefs } from "vue";
import type { FormInstance } from "element-plus";
import { ElMessage } from "element-plus";
import { login } from "~/api/login";
import { loginParams } from "~/types/login";
import { useRouter } from "vue-router";
const router = useRouter();

const ruleFormRef = ref<FormInstance>();
const ruleForm = reactive({
  username: "",
  passworld: "",
});

// 表单校验
const validatorTel = (rule: any, value: any, callback: any) => {
  if (!value) {
    return callback(new Error("请输入账号"));
  } else {
    callback();
  }
};

const validatePass = (rule: any, value: any, callback: any) => {
  if (value === "" || !value) {
    callback(new Error("请输入密码"));
  } else {
    callback();
  }
};
// validator form
const rules = reactive({
  passworld: [{ validator: validatePass, trigger: "blur", required: true }],
  username: [{ validator: validatorTel, trigger: "blur", required: true }],
});
const submitLogin = async (data: any) => {
  const { username, passworld } = data;
  const result = await login({
    username,
    passworld,
  });
  const { token } = result;
  localStorage.setItem("token", token);
  router.push("/home");
};
// 表单提交
const submitForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.validate((valid) => {
    if (valid) {
      // 登陆再次校验
      submitLogin(ruleForm);
    } else {
      ElMessage.error("请检查您输入的账号密码是否符合规则");
      return false;
    }
  });
};

// 重置表单
const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.resetFields();
};
</script>

<style scoped lang="scss">
.login_box {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  .form_box {
    width: 400px;
    height: 250px;
    background-color: #eee;
    border-radius: 15px;
    box-shadow: 2px 2px 5px #ddd;
    padding: 0 30px 0 30px;
  }
  .title {
    text-align: center;
    margin-bottom: 10px;
  }
  .demo-ruleForm {
    margin-top: calc(50% - 160px);
  }
  .login_btn {
    width: calc(100% / 2 - 8px);
  }
}
</style>
